//
//  LTHomeworkAfterClassVC.m
//  LearnTeacher
//
//  Created by zyl_macbookpro on 2024/4/18.
//

#import "LTHomeworkAfterClassVC.h"
#import "LTHomeworkTableViewCell.h"
#import "LTChooseClassView.h"
#import "LTDatePickerView.h"
#import "LTHomeworkAipManager.h"
@interface LTHomeworkAfterClassVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic ,strong)UITableView *tableView;

@property(nonatomic ,strong)UIView *topBtnView;

@property(nonatomic ,strong)NSArray *data;

@property(nonatomic ,strong)LTChooseClassView *classView;

@property(nonatomic ,strong)UIButton *classBtn;

@property(nonatomic ,strong)UIButton *timeBtn;

@property(nonatomic ,strong)LTDatePickerView *datePicker;

@property(nonatomic ,assign)int page;

@property(nonatomic ,strong)NSMutableArray *dataArray;

@property(nonatomic ,copy)NSString *startTime;

@property(nonatomic ,copy)NSString *classId;

@property(nonatomic ,strong)NSArray *classList;

@end

@implementation LTHomeworkAfterClassVC
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    [self renderNavBarWithBackgroundImage:[UIImage imageWithColor:rgba(74, 144, 246, 1)] backgroundColor:LTClearColor shadowColor:nil textAttributeDic:@{NSForegroundColorAttributeName:rgba(0, 0, 0, 1),NSFontAttributeName:MFontWithSize(17)} enableScrollEdgeAppearance:YES];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = rgba(247, 248, 250, 1);
    [self.view addSubview:self.topBtnView];
    self.classId = @"";
    self.dataArray = @[].mutableCopy;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.view addSubview:self.tableView];
        [self.view addSubview:self.classView];
        [self.view addSubview:self.datePicker];
    });
}
- (void)refresh
{
    self.page = 1;
    [self loadData];
}
- (void)loadMore
{
    self.page += 1;
    [self loadData];
}
- (void)loadData
{
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setObject:@"1" forKey:@"type"];
    [dic setObject:self.classId forKey:@"cid"];
    [dic setObject:@"1" forKey:@"start"];
    [dic setObject:[NSNumber numberWithInt:self.page] forKey:@"page"];
    [dic setObject:@"10" forKey:@"pageSIze"];
    [LTHomeworkAipManager getHomeworkListWithParam:dic result:^(NSDictionary * _Nonnull dic) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([dic[@"code"] doubleValue] == 1) {
            if (self.page == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:dic[@"data"]];
            [self.tableView reloadData];
         }else{
             [SVProgressHUD showImage:nil status:dic[@"msg"]];
         }
    }];
}
- (void)getGradeList
{
    [LTHomeworkAipManager getGradeListWithParam:@{} result:^(NSDictionary * _Nonnull dic) {
        self.classList = dic[@"data"];
        self.classView.dataArray =  self.classList;
        [self.classView openView];
    }];
}
- (void)chooseClick:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (!self.classView.hidden)[self.classView closeView];
    if (btn.tag == 10000) {
        btn.selected? [self getGradeList]:[self.classView closeView];
    }
    if (!self.classView.hidden)[self.datePicker dismissView];
    if (btn.tag == 10001) {
        btn.selected? [self.datePicker showView]:[self.datePicker dismissView];
    }
}

#pragma mark -UITableViewDelegate,UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LTHomeworkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LTHomeworkTableViewCell"];
    cell.data = self.dataArray[indexPath.row];
   
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 245;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *vc = [[UIViewController alloc] initWithClassName:@"LTHomeworkDetailVC"];
   // vc.paraObject = self.data[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, LTScreenWidth, self.view.height - 44) style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setBackgroundColor:LTClearColor];
        [_tableView setSeparatorColor:LTClearColor];
        WeakSelf(self);
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself refresh];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakself loadMore];
        }];
        [_tableView registerNib:[UINib nibWithNibName:@"LTHomeworkTableViewCell" bundle:nil] forCellReuseIdentifier:@"LTHomeworkTableViewCell"];
      
    }
    
    return _tableView;
}
- (UIView *)topBtnView
{
    if (!_topBtnView) {
        _topBtnView = [[UIView alloc] initWithFrame:CGRectMake(0,0, LTScreenWidth, 44)];
        _topBtnView.backgroundColor = LTWhiteColor;
        NSArray *title = @[@"全部班级",@"开始日期",@"只看本学期"];
        
        for (int i = 0; i < title.count; i++) {
            UIButton *monthLBtn = ({
                UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
                button.frame = CGRectMake((LTScreenWidth)/3.0*i ,2, (LTScreenWidth)/3.0, 44);
                [button setTitle:title[i] forState:UIControlStateNormal];
                [button setTitleColor:LTTextBlack forState:UIControlStateNormal];
                [button setTitleColor:rgba(41, 121, 255, 1) forState:UIControlStateSelected];
                
                [button addTarget:self action:@selector(chooseClick:) forControlEvents:UIControlEventTouchUpInside];
                button;
            });
            monthLBtn.titleLabel.font = RFontWithSize(13.f);
            monthLBtn.tag = i + 10000;
            [monthLBtn setImage:IMAGENAMED(@"ic_homework_arrow_down") forState:UIControlStateNormal];
            [monthLBtn setImage:IMAGENAMED(@"ic_homework_arrow_up") forState:UIControlStateSelected];
            [monthLBtn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleRight imageTitleSpace:4];
            if (i == 2) {
                [monthLBtn setImage:IMAGENAMED(@"ic_homework_term_selected_no") forState:UIControlStateNormal];
                [monthLBtn setImage:IMAGENAMED(@"ic_homework_term_selected") forState:UIControlStateSelected];
                [monthLBtn layoutButtonWithEdgeInsetsStyle:LButtonEdgeInsetsStyleLeft imageTitleSpace:8];
            }
            if (i == 0) {
                self.classBtn = monthLBtn;
            }
            if (i == 1) {
                self.timeBtn = monthLBtn;
            }
            [_topBtnView addSubview:monthLBtn];
        }
        
    }
    return _topBtnView;
}
- (LTChooseClassView *)classView
{
    if (!_classView) {
        _classView = [[LTChooseClassView alloc] initWithFrame:CGRectMake(0, 44, LTScreenWidth, self.view.height - 44)];
        WeakSelf(self);
        _classView.block = ^(NSInteger index) {
            weakself.classBtn.selected = NO;
            weakself.classId = @"";
            if (index > 0) {
                NSDictionary *dic = weakself.classList[index - 1];
                weakself.classId  = [NSString stringWithFormat:@"%@",dic[@"id"]];
            }
            [weakself refresh];
        };
    }
    return _classView;
}
- (LTDatePickerView *)datePicker
{
    if (!_datePicker) {
        _datePicker = [[NSBundle mainBundle] loadNibNamed:@"LTDatePickerView" owner:nil options:nil].lastObject;
        [_datePicker setFrame:CGRectMake(0, 44, LTScreenWidth, self.view.height - 44)];
        [_datePicker setUI];
        WeakSelf(self);
        _datePicker.block = ^(NSString * _Nonnull date) {
            
            weakself.timeBtn.selected = NO;
            if (date.length > 0) {
                weakself.startTime = [date componentsSeparatedByString:@"+"].firstObject;
                [weakself refresh];
            }
           
        };
    }
    return _datePicker;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
